package org.gas.barapraja.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.gas.barapraja.db.entity.Account;
import org.gas.barapraja.db.entity.Token;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Parser class to parsing account from xml file using the SAX parser library
 * @author gilang
 * @see {@link org.xml.sax.helpers.DefaultHandler}
 */
class AccountParser extends DefaultHandler {

	private Account account;

	private String value;
	
	private static List<Account> accounts=new ArrayList<Account>();

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		value="";
		if(qName.equalsIgnoreCase("account")){
			this.account=new Account();
		}
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.equalsIgnoreCase("systemId")){
			account.setSystemId(value);
		}else if(qName.equalsIgnoreCase("password")){
			account.setPassword(value);
		}else if(qName.equalsIgnoreCase("companyNumber")){
			account.setCompanyNumber(value);
		}else if(qName.equalsIgnoreCase("token")){
			Token token=new Token();
			token.setTokenValue(Integer.parseInt(value));
			account.setToken(token);
		}
		accounts.add(account);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		value=new String(ch,start,length);
	}

	/**
	 * Getting the Account object of parsed file
	 * @param key - the <code>Account</code>'s <code>systemId</code>
	 * @return the accounts
	 */
	
	public Account getObject(String key){
		Account acc=new Account();
		Iterator<Account> iter=accounts.iterator();
		while(iter.hasNext()){
			Account found=iter.next();
			if(found.getSystemId().equalsIgnoreCase(key)){
				acc=found;
				break;
			}else
				acc=null;
		}
		return acc;
	}
	
	public Account getAccountByAddress(String number){
		Account acc=new Account();
		Iterator<Account> iter=accounts.iterator();
		while(iter.hasNext()){
			Account found=iter.next();
			if(found.getCompanyNumber().equalsIgnoreCase(number)){
				acc=found;
				break;
			}else
				acc=null;
		}
		return acc;
	}
	
	/**
	 * Parsing the file contained with account list
	 * @param file - the xml file path
	 */
	public void parseFile(String file){
		SAXParserFactory parserFactory=SAXParserFactory.newInstance();
		SAXParser parser=null;
		try {
			parser=parserFactory.newSAXParser();
			parser.parse(new File(file), this);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}


}
